package com.hand.demo.api.controller.v1;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import org.hzero.boot.file.FileClient;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.hzero.mybatis.helper.SecurityTokenHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.hand.demo.app.service.HhrTaBasicInfoService;
import com.hand.demo.domain.entity.HhrTaBasicInfo;
import com.hand.demo.domain.repository.HhrTaBasicInfoRepository;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 简历基本信息解析表(HhrTaBasicInfo)表控制层
 *
 * @author
 * @since 2025-06-16 12:16:55
 */

@RestController("hhrTaBasicInfoController.v1")
@RequestMapping("/v1/{organizationId}/hhr-ta-basic-infos")
public class HhrTaBasicInfoController extends BaseController {

    @Autowired
    private HhrTaBasicInfoRepository hhrTaBasicInfoRepository;

    @Autowired
    private HhrTaBasicInfoService hhrTaBasicInfoService;

    @Autowired
    private FileClient fileClient;

    @ApiOperation(value = "简历基本信息解析表列表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping
    public ResponseEntity<Page<HhrTaBasicInfo>> list(HhrTaBasicInfo hhrTaBasicInfo, @PathVariable Long organizationId,
                                                     @ApiIgnore @SortDefault(value = HhrTaBasicInfo.FIELD_ID,
                                                             direction = Sort.Direction.DESC) PageRequest pageRequest) {
        Page<HhrTaBasicInfo> list = hhrTaBasicInfoService.selectList(pageRequest, hhrTaBasicInfo);
        return Results.success(list);
    }


    @ApiOperation(value = "解析简历")
    @Permission(level = ResourceLevel.ORGANIZATION, permissionLogin = true)
    @PostMapping("/parse")
    public ResponseEntity<String> parseAndSaveResume(@RequestBody String resumeJson, @PathVariable Long organizationId){
        hhrTaBasicInfoService.parseAndSaveResume(resumeJson, organizationId);
        return Results.success("简历解析成功");
    }

    @ApiOperation(value = "简历基本信息解析表明细")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/{id}/detail")
    public ResponseEntity<HhrTaBasicInfo> detail(@PathVariable Long id) {
        HhrTaBasicInfo hhrTaBasicInfo = hhrTaBasicInfoRepository.selectByPrimary(id);
        return Results.success(hhrTaBasicInfo);
    }

    @ApiOperation(value = "创建或更新简历基本信息解析表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping
    public ResponseEntity<List<HhrTaBasicInfo>> save(@PathVariable Long organizationId, @RequestBody List<HhrTaBasicInfo> hhrTaBasicInfos) {
        validObject(hhrTaBasicInfos);
        SecurityTokenHelper.validTokenIgnoreInsert(hhrTaBasicInfos);
        hhrTaBasicInfos.forEach(item -> item.setTenantId(organizationId));
        hhrTaBasicInfoService.saveData(hhrTaBasicInfos);
        return Results.success(hhrTaBasicInfos);
    }

    @ApiOperation(value = "删除简历基本信息解析表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping
    public ResponseEntity<?> remove(@RequestBody List<HhrTaBasicInfo> hhrTaBasicInfos) {
        SecurityTokenHelper.validToken(hhrTaBasicInfos);
        hhrTaBasicInfoRepository.batchDeleteByPrimaryKey(hhrTaBasicInfos);
        return Results.success();
    }

}

